package com.s7.widget.recycler.adapter;

import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;

import com.s7.widget.recycler.holder.BaseHolder;
import com.s7.widget.recycler.holder.BaseViewHolder;

import java.util.List;

public abstract class BaseMultiStyleAdapter<T> extends CompatRecyclerAdapter<T> {

    @NonNull
    @Override
    public BaseHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = getView(parent, getLayoutId()[viewType]);
        return new BaseViewHolder<>(view, viewType);
    }

    @Override
    public void onBindViewHolder(@NonNull BaseHolder holder, int position) {
        holder.setRealPosition(position);
        if (holder instanceof BaseViewHolder) {
            initClickListener((BaseViewHolder)holder, datas.get(position));
            ((BaseViewHolder) holder).setData(datas.get(position));
            bindData((BaseViewHolder) holder, position, datas.get(position));
        }
    }

    @Override
    public int getItemCount() {
        return datas == null ? 0 : datas.size();
    }

    protected abstract int[] getLayoutId();

    protected abstract void bindData(BaseViewHolder holder, int position, T data);

    public void onRefresh(List<T> datas) {
        initData();
        this.datas.clear();
        if (datas != null) {
            this.datas.addAll(datas);
        }
        notifyDataSetChanged();
    }

}
